package api

import (
	"context"
	"zhonghui/console/models"
	"zhonghui/console/structs"
	"zhonghui/console/warehouse/helperx"
	"zhonghui/console/warehouse/logx"
	"zhonghui/console/warehouse/respx"

	"github.com/gin-gonic/gin"
)

type GetMpConfigListRequest struct {
	structs.PageBase
	CorpId   string `form:"corpid" binding:"required" validate:"max=64"`
	ConfigId string `form:"config_id"  validate:"max=64"`
}

func (server *Server) GetMpConfigList(c *gin.Context) {

	req := GetMpConfigListRequest{}
	if err := c.ShouldBindQuery(&req); err != nil {
		logx.Zap().Errorw("GetMpConfigList ShouldBindQuery", "err", err)
		respx.Error(c, "parameter error", err)
		return
	}

	if req.ConfigId != "" {
		conf, err := server.store.GetMiniConfig(context.Background(), models.GetMiniConfigParams{
			Corpid:   req.CorpId,
			ConfigID: req.ConfigId,
		})
		if err != nil {
			logx.Zap().Errorw("GetMpConfigList GetMiniConfig", "err", err)
			respx.Error(c, "get mp config list err", err)
			return
		}
		t := int64(0)
		if conf.ID != 0 {
			t = 1
		}
		respx.Success(c, []models.ConsoleMiniConfig{conf}, helperx.MakeNormalPagerResp(t, int(req.PageSize)))
		return
	}

	total, err := server.store.GetMiniConfigListCount(context.Background(), req.CorpId)
	if err != nil {
		logx.Zap().Errorw("GetMpConfigList GetMiniConfigListCount", "err", err)
		respx.Error(c, "get mp config list err", err)
		return
	}

	list, err := server.store.GetMiniConfigList(context.Background(), models.GetMiniConfigListParams{
		Corpid: req.CorpId,
		Offset: req.Offset(),
		Limit:  req.PageSize,
	})

	if err != nil {
		logx.Zap().Errorw("GetMpConfigList GetMiniConfigList", "err", err)
		respx.Error(c, "get mp config list err", err)
		return
	}

	respx.Success(c, list, helperx.MakeNormalPagerResp(total, int(req.PageSize)))
}

type CreateMpConfigRequest struct {
	Title     string `json:"title" binding:"required" validate:"max=64"`
	Content   string `json:"content" binding:"required" validate:"max=256"`
	Guarantee string `json:"guarantee" binding:"required" validate:"max=256"`
	ConfigId  string `json:"config_id" binding:"required" validate:"max=64"`
	CorpId    string `json:"corp_id" binding:"required" validate:"max=64"`
}

func (server *Server) CreateMpConfig(c *gin.Context) {

	req := CreateMpConfigRequest{}
	if err := c.ShouldBindJSON(&req); err != nil {
		logx.Zap().Errorw("CreateMpConfig ShouldBindJSON", "err", err)
		respx.Error(c, "parameter error", err)
		return
	}

	err := server.store.InsertMiniConfig(context.Background(), models.InsertMiniConfigParams{
		Title:       req.Title,
		Content:     req.Content,
		Guarantee:   req.Guarantee,
		ConfigID:    req.ConfigId,
		Status:      "valid",
		Corpid:      req.CorpId,
		Title_2:     req.Title,
		Content_2:   req.Content,
		Guarantee_2: req.Guarantee,
		Status_2:    "valid",
	})

	if err != nil {
		logx.Zap().Errorw("CreateMpConfig CreateMiniConfig", "err", err)
		respx.Error(c, "create mp config err", err)
		return
	}

	respx.Success(c, "ok", nil)
}

type UpdateMpConfigRequest struct {
	ID        int32  `json:"id" binding:"required"`
	Title     string `json:"title" binding:"required" validate:"max=64"`
	Content   string `json:"content" binding:"required" validate:"max=256"`
	Guarantee string `json:"guarantee" binding:"required" validate:"max=256"`
	ConfigId  string `json:"config_id" binding:"required" validate:"max=64"`
	CorpId    string `json:"corp_id" binding:"required" validate:"max=64"`
}

func (server *Server) UpdateMpConfig(c *gin.Context) {

	req := UpdateMpConfigRequest{}
	if err := c.ShouldBindJSON(&req); err != nil {
		logx.Zap().Errorw("UpdateMpConfig ShouldBindJSON", "err", err)
		respx.Error(c, "parameter error", err)
		return
	}

	err := server.store.UpdateMiniConfig(context.Background(), models.UpdateMiniConfigParams{
		ID:        req.ID,
		Title:     req.Title,
		Content:   req.Content,
		Guarantee: req.Guarantee,
		ConfigID:  req.ConfigId,
		Corpid:    req.CorpId,
		Status:    "valid",
	})

	if err != nil {
		logx.Zap().Errorw("UpdateMpConfig UpdateMiniConfig", "err", err)
		respx.Error(c, "update mp config err", err)
		return
	}

	respx.Success(c, "ok", nil)

}

type GetMpConfigByIDRequest struct {
	ConfigId string `uri:"config_id" binding:"required" validate:"max=64"`
}
type GetMpConfigByIDRequest2 struct {
	CorpId string `form:"corp_id" binding:"required" validate:"max=128"`
}

type GetMpConfigByIDResponse struct {
	Title     string `json:"title"`
	Content   string `json:"content"`
	Guarantee string `json:"guarantee"`
	QrCode    string `json:"qr_code"`
	ConfigId  string `json:"config_id"`
}

func (server *Server) GetMpConfigByID(c *gin.Context) {
	var req GetMpConfigByIDRequest
	if err := c.ShouldBindUri(&req); err != nil {
		respx.Error(c, "params err 1", err)
		return
	}

	var req2 GetMpConfigByIDRequest2
	if err := c.ShouldBindQuery(&req2); err != nil {
		respx.Error(c, "params err 2"+err.Error(), err)
		return
	}

	configs, err := server.store.GetDefaultMiniConfig(context.Background(), models.GetDefaultMiniConfigParams{
		ConfigID: req.ConfigId,
		Corpid:   req2.CorpId,
	})
	if err != nil {
		logx.Zap().Errorw("GetMpConfigByID GetMiniConfigByID", "err", err)
		respx.Error(c, "get mp config err", err)
		return
	}
	result := GetMpConfigByIDResponse{}
	dr := GetMpConfigByIDResponse{}
	for _, v := range configs {
		if v.ConfigID == "default" {
			dr.ConfigId = v.ConfigID
			dr.Title = v.Title
			dr.Content = v.Content
			dr.Guarantee = v.Guarantee
		} else {
			result.ConfigId = v.ConfigID
			result.Title = v.Title
			result.Content = v.Content
			result.Guarantee = v.Guarantee
		}
	}
	if result.ConfigId == "" {
		result = dr
	}
	if result.ConfigId != "default" {
		qrCode, err := server.store.GetContactQrCode(context.Background(), req.ConfigId)
		if err != nil {
			logx.Zap().Errorw("GetMpConfigByID GetContactQrCode", "err", err)
			respx.Error(c, "get mp config err", err)
			return
		}

		result.QrCode = qrCode
	}

	respx.Success(c, result, nil)
}
